import {selectWishes, putWish, getWishesOperate ,updateWishes} from "@/base/api/BannersApi";
import {Page} from "@/base/Page";
import {Company} from "@/base/model/Company";
import {getAgentList} from "@/base/api/AgentApi";
import {deepAssign} from "@/base/util/Objects";
import {getCompanyList} from '@/base/api/CompanyApi';
import {Agent} from "@/base/model/Agent";

export class FormBloc {
  model: any = {
    id: 0,
    companyId: 0,
    agentId: 1,
    wishName: '',
    imageurl: '',
    isAble: 0,
    type: 'IMAGE',
    startTime:'',
    endTime:'',
    screenOpeningTime:10,
    linkUrl:'',
    status:0
  };
  loading = false;
  hasFormInfo = false;
  fileList: Array<any> = []
  num: number = 0;
  sortList: any = [];
  companyList: any = []
  isDetail = false;
  type: string | undefined;
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);

  load(id: number) {
    return getWishesOperate({id: id})
      .then((res) => {
        console.log(res.data,111)
        this.model = res.data;
        // this.model.agentId = res.data.agentId;
        // this.model.wishName = res.data.wishName;
        this.model.imageurl = res.data.wishUrl;
        return res;
      }).finally(() => {
        this.hasFormInfo = true;
      })
  }
  getAgentList(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getAgentList({
      pageIndex,
      pageSize,
      sortDirection,
      companyId: this.model.companyId
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = {id: 0, agentName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getCompanyList(pageIndex: number,
                 pageSize: number,
                 sortDirection: string) {
    return getCompanyList({pageIndex, pageSize, sortDirection, name: '', createBegin: ''})
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = {companyId: 0, companyName: ''}
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }

  save(model:any) {
    this.loading = true;
    return updateWishes({
      id: model.id,
      wishName: model.wishName,
      wishUrl: model.imageurl[0],
      isAble: model.isAble,
      type: model.type,
      startTime: this.formatDate(model.startTime),
      endTime: this.formatDate(model.endTime),
      screenOpeningTime:model.screenOpeningTime,
      linkUrl:model.linkUrl,
      status:model.status,
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
  
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
  
}
